# 导入蓝图模块
from flask import Blueprint

# 导入基础方法
from flask import request,jsonify,send_from_directory

# 实例化蓝图

course = Blueprint("course",__name__)

# 导入数据库操作类
from db import Db

import redis

# 导入树工具类
from mytree import MyTree

# 导入分页类
from mypage import Page



# 模糊提示匹配
@course.route('/keyword/',methods=['GET'])
def keyword():

	keyword = request.args.get("keyword")

	keyword = keyword.strip()

	print(keyword)

	if keyword == "":
		return jsonify([])

	id = request.args.get("id")

	mydb = Db()

	# 判断分隔符
	if keyword.find(" ") == -1:
		
		res = mydb.get_all(" select `keyword` from `search` where `keyword` like '%%%s%%' and uid = %s " % (keyword,id))

	else:

		# 至少两个关键字

		keyword = keyword.split(" ")
		sql = ""
		# 遍历关键字列表
		for item in keyword:

			sql += " keyword like '%%%s%%' or" % item

		sql = sql.rstrip("or")

		print(sql)

		sql = " select `keyword` from `search` where (" + sql + ") and uid = %s " % id

		res = mydb.get_all(sql)

		return jsonify(res)


	return jsonify(res)



# 课程删除接口
@course.route('/delcourse/',methods=['DELETE'])
def delcourse():

	id = request.args.get("id")

	mydb = Db()

	mydb.delete(" delete from course where id in (%s) " % id)

	return {"code":200,'msg':"删除成功"}


# 课程修改
@course.route('/updatecourse/',methods=['PUT'])
def updatecourse():

	id = request.form.get("id")
	fid = request.form.get("fid")
	title = request.form.get("title")
	desc = request.form.get("desc")
	params = request.form.get("params",None)

	mydb = Db()

	if params:

		mydb.update(" update `course` set `cid`=%s,`title`='%s',`desc`='%s',`params`='%s' where id = %s " % (fid,title,desc,params,id))
	else:
		mydb.update(" update `course` set `cid`=%s,`title`='%s',`desc`='%s' where id = %s " % (fid,title,desc,id))


	return {'code':200,'msg':"修改课程成功"}


import re

# 关键字高亮
def keyword_highlight(keyword,data):


	if keyword != "":

		# 遍历数据
		for item in data:

			title = item["title"]

			# 替换逻辑
			res = re.sub("%s" % keyword,"<font>\g<0></font>",title,flags=re.I|re.M)

			item["title_highlight"] = res

		print(data)

		return data
	else:

		# 遍历数据
		for item in data:
			item["title_highlight"] = item["title"]


		return data




# 课程详情接口
@course.route('/getcourse/',methods=['GET'])
def getcourse():

	fid = request.args.get("fid")

	uid = request.args.get("id",None)

	page = request.args.get("page",1)
	num = request.args.get("num",2)

	# 关键字
	keyword = request.args.get("keyword",None)

	keyword = keyword.strip()

	mydb = Db()

	if uid:

		sql = " select count(1) as total from course where uid = %s  " % uid

		sql2 = " "

		# 如果只有一个关键字
		if keyword.find(" ") == -1:

			if keyword:

				sql += " and title like '%%%s%%' " % keyword
				sql2 = " and a.title like '%%%s%%' " % keyword

			res = mydb.get_one(sql)

			sql = " select a.id,a.title,case a.state when 0 then '待审' when 1 then '审核通过' when 2 then '审核拒绝' end as state,b.name,b.id as cateid from course a left join cate b on a.cid = b.id where a.uid = %s  %s order by a.id desc " % (uid,sql2)
		else:
			# 至少有两个关键字
			keyword = keyword.split(" ")
			sql3 = ""
			for item in keyword:

				sql3 += "  title like '%%%s%%' or" % item

			sql3 = sql3.rstrip("or")

			# 拼接个数sql

			sql += " and (%s) " % sql3

			res = mydb.get_one(sql)

			# 拼接 数据查询语句

			sql = " select a.id,a.title,case a.state when 0 then '待审' when 1 then '审核通过' when 2 then '审核拒绝' end as state,b.name,b.id as cateid from course a left join cate b on a.cid = b.id where a.uid = %s and (%s) order by a.id desc " % (uid,sql3)

		# 实例化对象
		page = Page(res['total'],page,num)

		# 获取多少页
		page_num = page.page_num()

		# 获取数据
		res = page.get_data(sql)


		# 高亮关键字
		keyword_highlight(keyword,res)

		return {"code":200,"page_num":page_num,"data":res}


	else:

		res = mydb.get_one(" select * from `course` where id = %s order by id desc " % fid)

	return jsonify(res)





# 获取分类详情
@course.route('/getcate/',methods=['GET'])
def getcate():

	fid = request.args.get("fid")

	mydb = Db()

	cate = mydb.get_one(" select `params` from `cate` where id = %s  " % fid)

	return jsonify(cate)



#添加课程
@course.route('/addcourse/',methods=['POST'])
def addcourse():

	fid = request.form.get("fid")
	title = request.form.get("title")
	desc = request.form.get("desc")
	uid = request.form.get("id")

	params = request.form.get("params",None)

	mydb = Db()

	# 判断是否有特定参数

	sql = " insert into `course` (`cid`,`title`,`desc`,`uid`) values (%s,'%s','%s',%s)" % (fid,title,desc,uid)

	if params:

		sql = " insert into `course` (`cid`,`title`,`desc`,`uid`,`params`) values (%s,'%s','%s',%s,'%s')" % (fid,title,desc,uid,params)

	mydb.insert(sql)

	course = mydb.get_one(" select `id` from `course` where `title` = '%s'  " % title)

	return {"code":200,"msg":"添加课程成功","cid":course['id']}

#添加课程分类
@course.route('/addcate/',methods=['POST'])
def addcate():

	fid = request.form.get("fid")
	name = request.form.get("name")
	params = request.form.get("params")

	mydb = Db()

	mydb.insert(" insert into `cate` (`fid`,`name`,`params`) values (%s,'%s','%s')" % (fid,name,params))

	return {"code":200,"msg":"添加分类成功"}


# 课程分类
@course.route('/catelist/',methods=['GET'])
def catelist():

	id = request.args.get("id",None)

	# 获取数据
	mydb = Db()

	# sql
	sql = " select * from `cate` "

	if id:

		sql += " where id in (%s) " % id

	# 查询
	mydata = mydb.get_all(sql)

	# 实例化
	tree = MyTree()

	# 调用层级结构方法

	data = tree.get_tree(mydata)


	return jsonify(data)



